﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppManagement
{
    #region Using
    using DevComponents.DotNetBar;
    using AppManagement.UsersManager;
    using AppManagement.Helpers;
    using AppManagement.Repositories;
    using AppManagement.UserManagers; 
    #endregion

    public partial class MainForm : Office2007Form 
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void menQuanLyChucNang_Click(object sender, EventArgs e)
        {
            FrmQuanLyChucNang frm = new FrmQuanLyChucNang();
            frm.ShowDialog();
        }

        private void menQuanLyChucNangNhomNguoiDung_Click(object sender, EventArgs e)
        {
            frmQuanLyChucNangNhomNguoiDung frm = new frmQuanLyChucNangNhomNguoiDung();
            frm.ShowDialog();
        }

        private void menQuanLyLoaiNguoiDung_Click(object sender, EventArgs e)
        {
            FrmQuanLyLoaiNguoiDung frm = new FrmQuanLyLoaiNguoiDung();
            frm.ShowDialog();
        }

        private void menQuanLyNguoiDung_Click(object sender, EventArgs e)
        {
            FrmQuanLyNguoiDung frm = new FrmQuanLyNguoiDung();
            frm.ShowDialog();
        }

        private void menDangXuat_Click(object sender, EventArgs e)
        {
            menDangNhap.Enabled = true;
            GlobalVars.g_CurrentUser = null;

            FrmDangNhap frm = new FrmDangNhap();
            frm.Handler += ValidateLogin;
            frm.ShowDialog();
        }

        public void ValidateLogin(object sender, EventArgs e)
        {
            bool success = true;
            string infoText = "";
            var eventType = (TransferSimpleDataEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            string username = comp[0].Trim();
            string password = comp[1].Trim();
            string PasswordEnc = Encryption.Encrypt(password);

            GlobalVars.g_CurrentUser = NguoiDungRepository.SelectByUserName(username);

            if (GlobalVars.g_CurrentUser == null)
            {
                success = false;
                infoText = "Người dùng này chưa có trong hệ thống";
            }
            else
            {
                GlobalVars.g_CurrentUser = NguoiDungRepository.SelectByUserNameAndPassword(username, PasswordEnc);
                if (GlobalVars.g_CurrentUser == null)
                {
                    success = false;
                    infoText = "Bạn đã nhập sai mật khẩu";
                }
                else
                {
                    infoText = "Chào bạn  " + GlobalVars.g_CurrentUser.TenDangNhap + "  vào hệ thống !";
                }
            }

            if (!success)
            {
                MessageBoxEx.Show(infoText, "Log In", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                // Re-login
                FrmDangNhap frm = new FrmDangNhap();
                frm.Handler += ValidateLogin;
                frm.ShowDialog();
            }
            else
            {
                if (GlobalVars.g_CurrentUser.LoaiNguoiDung.TenLND.ToUpper() != "NHÂN VIÊN")
                {
                    // Admin, enabel chuc nang quan tri nguoi dung
                    menQuanLyChucNang.Enabled = true;
                    menQuanLyChucNangNhomNguoiDung.Enabled = true;
                    menQuanLyLoaiNguoiDung.Enabled = true;
                    menQuanLyNguoiDung.Enabled = true;
                }
                else
                {
                    // Nhan vien
                    menQuanLyChucNang.Enabled = false;
                    menQuanLyChucNangNhomNguoiDung.Enabled = false;
                    menQuanLyLoaiNguoiDung.Enabled = false;
                    menQuanLyNguoiDung.Enabled = false;
                }

                menDangNhap.Enabled = false;

                // Get current user
                GlobalVars.g_CurrentUser = NguoiDungRepository.SelectByUserName(username);
                // Mapping name of controls with specifed enums
                var dictFucns = GlobalUserManagers.InitFuncsMapping();
                // Store list of functionalities that user allow using
                var funcsAllowUsing = GlobalUserManagers.InitFuncsAllowUsing();

                // Disable some functions that user not allow using
                foreach (var item in dictFucns)
                {
                    foreach (var item2 in funcsAllowUsing)
                    {
                        if (item.Value == item2)
                        {
                            GlobalUserManagers.EnableFuncAllowUsing(pnlMain, item.Key);
                            break;
                        }
                    }
                }

                MessageBoxEx.Show(infoText);
            }
        }

        private void menDangNhap_Click(object sender, EventArgs e)
        {
            FrmDangNhap frm = new FrmDangNhap();
            frm.Handler += ValidateLogin;
            frm.ShowDialog();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string ErrorText = "";

            if (!System.IO.File.Exists(GlobalVars.g_strPathConfig))
            {
                FrmKetNoiCSDL frm = new FrmKetNoiCSDL();
                frm.ShowDialog();
            }
            else
            {
                XMLProvider _provider = new XMLProvider();
                // Load config file to get infos that used to connect to DB
                _provider.GetConnectionInfo(ref ErrorText);
                if (ErrorText != "")
                {
                    MessageBoxEx.Show(ErrorText, "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                    // Re-create connection infos
                    FrmKetNoiCSDL frm = new FrmKetNoiCSDL();
                    frm.ShowDialog();
                }
                else
                {
                    // Get connection string successfull, then open connection
                    var lstNguoiDung = NguoiDungRepository.SelectAll();
                    if (lstNguoiDung.Count > 0)
                    {
                        FrmDangNhap frm = new FrmDangNhap();
                        frm.Handler += ValidateLogin;
                        frm.ShowDialog();
                    }
                    else
                    {
                        // Chua co nguoi dung nao trong he thong, nguoi dung dau tien se giu vai tro la admin
                        menDangNhap.Enabled = false;
                        menDangXuat.Enabled = false;
                        menDoiMatKhau.Enabled = false;
                        menQuanLyChucNang.Enabled = true;
                        menQuanLyChucNangNhomNguoiDung.Enabled = true;
                        menQuanLyLoaiNguoiDung.Enabled = true;
                        menQuanLyNguoiDung.Enabled = true;
                    }
                }
            }
        }

        private void menDoiMatKhau_Click(object sender, EventArgs e)
        {
            FrmDoiMatKhau frm = new FrmDoiMatKhau();
            frm.ShowDialog();
        }

        private void menThoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
